const db_cms = require('../config').db_cms;
const db_files = require('../config').db_files;
const uuid = require('node-uuid');
/** 发布音乐 */
module.exports.publish_music_info = publish_music_info;
async function publish_music_info(owner_id, albums_id, singer, lyricist, composer, music_audio_id, music_cover_id, music_title, music_lrc, music_tags, music_type, music_status) {
    /** 查询专辑是否存在 状态是否正常 是否是属于该用户 */
    albums_id = db_cms.escape(albums_id);
    owner_id = db_cms.escape(owner_id);
    let sql = 'select albums_id from albums_info where albums_id=' + albums_id + ' and albums_type=0 and albums_status=0 and owner_id=' + owner_id;
    let albums_flag = await db_cms.queryOne(sql);
    if (albums_flag == null) {
        return [false, '专辑操作出错!'];
    }
    /** 查询音频是否存在 是否是属于该用户 */
    sql = 'SELECT audio_time FROM `audio_info` where audio_id=' + music_audio_id + ' and owner_id=' + owner_id;
    let audio_info = await db_files.queryOne(sql);
    if (audio_info == null) {
        return [false, '音频不存在!'];
    }
    let music_time = audio_info['audio_time'];
    let music_id_uuid = "mus_" + uuid.v1().replace(/\-/g, '').substring(0, 28);
    singer = db_cms.escape(singer);
    lyricist = db_cms.escape(lyricist);
    composer = db_cms.escape(composer);
    music_title = db_cms.escape(music_title);
    music_lrc = db_cms.escape(music_lrc);
    music_tags = db_cms.escape(music_tags);
    let music_id = db_cms.escape(music_id_uuid);

    let connection = await db_cms.getConnection();
    if (connection == null) {
        console.trace('trace connection fail');
        return [false, '服务连接异常!'];
    }
    let flag = true;
    try {
        await connection.beginTransaction();

        sql = 'INSERT INTO music_info(music_id,albums_id,owner_id,singer,lyricist,composer,music_audio_id,music_time,music_cover_id,music_title,music_lrc,music_tags,music_type,music_status,create_time,update_time)' +
            ' VALUE(' + music_id + ',' + albums_id + ',' + owner_id + ',' + singer + ',' + lyricist + ',' + composer + ',' + music_audio_id + ',' + music_time + ',' + music_cover_id + ',' + music_title + ',' + music_lrc + ',' + music_tags + ',' + music_type + ',' + music_status + ',UNIX_TIMESTAMP(NOW()),UNIX_TIMESTAMP(NOW()));';
        await connection.query(sql);
        sql = 'UPDATE albums_info SET music_num=music_num+1 where albums_id=' + albums_id;
        await connection.query(sql);
        /** 音乐专辑添加关联 */
        sql = 'INSERT INTO music_albums(music_id,albums_id,audio_id,music_title,singer,create_time) VALUE(' + music_id + ',' + albums_id + ',' + music_audio_id + ',' + music_title + ',' + singer + ',UNIX_TIMESTAMP(NOW()))';
        await connection.query(sql);

        await connection.commit();
    } catch (err) {
        /**
         * TODO
         * 日志系统
         */
        console.trace(err);
        await connection.rollback();
        flag = false;
    }
    if (connection) {
        db_cms.releaseConnection(connection);
    }
    return [flag, music_id_uuid];
}

/** 音乐详情 */
module.exports.get_music_detail = get_music_detail;
async function get_music_detail(owner_id, music_id) {
    /** 查询音乐信息 */
    /** 查询专辑信息 
     * albums_cover_id  专辑封面
     * albums_name      专辑标题
     * follow_num       专辑关注数量
    */
    /** 查询音乐交互信息 */
    owner_id = db_cms.escape(owner_id);
    music_id = db_cms.escape(music_id);
    // sql = 'SELECT a.*,b.cover_id albums_cover_id,b.albums_name,b.follow_num FROM music_info a,albums_info b where a.music_id=' + music_id + ' and a.owner_id=' + owner_id + ' and a.albums_id=b.albums_id';
    sql = 'SELECT a.*,b.cover_id albums_cover_id,b.albums_name,b.follow_num FROM music_info a,albums_info b where a.music_id=' + music_id + ' and a.albums_id=b.albums_id';
    music_info = await db_cms.queryOne(sql);
    
    return music_info;
}

/** 点赞/取消点赞 */
module.exports.update_music_like_num = update_music_like_num;
async function update_music_like_num(music_id, num_flag) {
    music_id = db_cms.escape(music_id);
    let sql = 'UPDATE music_info SET like_num=like_num +' + num_flag + ' WHERE music_id=' + music_id;
    let flag = await db_cms.queryNum(sql);
    return flag < 1 ? false : true;
}

/** 音乐是否存在 */
module.exports.get_music_like_num = get_music_like_num;
async function get_music_like_num(music_id) {
    music_id = db_cms.escape(music_id);
    let sql = 'select like_num from music_info WHERE music_id=' + music_id;
    let flag = await db_cms.queryOne(sql);
    if (flag != null) {
        return flag['like_num'];
    }
    return null;
}

/** 更新音频时长 */
module.exports.update_music_time_by_audio_id = update_music_time_by_audio_id;
async function update_music_time_by_audio_id(music_audio_id, music_time) {
    let sql = 'UPDATE music_info SET music_time=' + music_time + ' WHERE music_audio_id=' + music_audio_id;
    let flag = await db_cms.queryNum(sql);
    return flag < 1 ? [false, "修改失败!"] : [true, "修改成功!"];
}


module.exports.query_single_music_list = query_single_music_list;
async function query_single_music_list(begin, plen, type) {
    if (type == 1) {
        return await query_news_music_list(begin, plen);
    } else if (type == 2) {
        return await query_hot_music_list(begin, plen);
    } else if (type == 3) {
        return await query_original_music_list(begin, plen);
    } else if (type == 4) {
        return await query_cover_music_list(begin, plen);
    } else if (type == 5) {
        return await query_music_demo_list(begin, plen);
    }
}

/** 查询最新的歌曲列表 */
module.exports.query_news_music_list = query_news_music_list;
async function query_news_music_list(begin, plen) {
    let sql = 'SELECT m.music_cover_id,m.music_audio_id,m.music_id,m.music_title,m.comment_num,m.singer,m.albums_id,a.albums_name FROM `music_info` m,`albums_info` a WHERE m.albums_id=a.albums_id and m.music_status=0 ORDER BY m.create_time DESC LIMIT ' + begin + ',' + plen;
    let music_list = await db_cms.query(sql);
    if (music_list instanceof Array && music_list.length > 0) {
        return music_list;
    }
    return [];
}

/** 查询歌曲小样列表 */
module.exports.query_music_demo_list = query_music_demo_list;
async function query_music_demo_list(begin, plen) {
    let sql = 'SELECT m.music_cover_id,m.music_audio_id,m.music_id,m.music_title,m.comment_num,m.singer,m.albums_id,a.albums_name FROM `music_info` m,`albums_info` a WHERE m.albums_id=a.albums_id and m.music_status=0 AND a.albums_tags LIKE "%小样%" ORDER BY m.like_num DESC LIMIT ' + begin + ',' + plen;
    let music_list = await db_cms.query(sql);
    if (music_list instanceof Array && music_list.length > 0) {
        return music_list;
    }
    return [];
}

/** 查询原创的歌曲列表 */
module.exports.query_original_music_list = query_original_music_list;
async function query_original_music_list(begin, plen) {
    let sql = 'SELECT m.music_cover_id,m.music_audio_id,m.music_id,m.music_title,m.comment_num,m.singer,m.albums_id,a.albums_name FROM `music_info` m,`albums_info` a WHERE m.music_type=1 and m.music_status=0 and m.albums_id=a.albums_id ORDER BY m.like_num DESC LIMIT ' + begin + ',' + plen;
    let music_list = await db_cms.query(sql);
    if (music_list instanceof Array && music_list.length > 0) {
        return music_list;
    }
    return [];
}

/** 查询翻唱的歌曲列表 */
module.exports.query_cover_music_list = query_cover_music_list;
async function query_cover_music_list(begin, plen) {
    let sql = 'SELECT m.music_cover_id,m.music_audio_id,m.music_id,m.music_title,m.comment_num,m.singer,m.albums_id,a.albums_name FROM `music_info` m,`albums_info` a WHERE m.music_type=0 and m.music_status=0 and m.albums_id=a.albums_id ORDER BY m.like_num DESC LIMIT ' + begin + ',' + plen;
    let music_list = await db_cms.query(sql);
    if (music_list instanceof Array && music_list.length > 0) {
        return music_list;
    }
    return [];
}

/** 查询最热的歌曲列表 评论最多的 */
module.exports.query_hot_music_list = query_hot_music_list;
async function query_hot_music_list(begin, plen) {
    let sql = 'SELECT m.music_cover_id,m.music_audio_id,m.music_id,m.music_title,m.comment_num,m.singer,m.albums_id,a.albums_name FROM `music_info` m,`albums_info` a WHERE m.albums_id=a.albums_id and m.music_status=0 ORDER BY m.comment_num DESC LIMIT ' + begin + ',' + plen;
    let music_list = await db_cms.query(sql);
    if (music_list instanceof Array && music_list.length > 0) {
        return music_list;
    }
    return [];
}


/** 查询推荐的歌曲列表 */
module.exports.query_recommend_music_list = query_recommend_music_list;
async function query_recommend_music_list(len) {
    let sql = 'SELECT music_id,music_title,music_cover_id,music_audio_id,singer FROM `music_info` WHERE music_status=0 ORDER BY favor_num DESC limit ' + len;
    let music_list = await db_cms.query(sql);
    if (music_list instanceof Array && music_list.length > 0) {
        return music_list;
    }
    return [];
}


/** 查询最新的歌曲列表 */
module.exports.search_music_list_by_music_title = search_music_list;
async function search_music_list(begin, plen, music_title) {
    let sql = 'SELECT m.music_cover_id,m.music_audio_id,m.music_id,m.music_title,m.comment_num,m.singer,m.albums_id,a.albums_name FROM `music_info` m,`albums_info` a WHERE m.music_title LIKE "%' + music_title + '%" AND m.albums_id=a.albums_id and m.music_status=0 ORDER BY m.like_num DESC LIMIT ' + begin + ',' + plen;
    let music_list = await db_cms.query(sql);
    if (music_list instanceof Array && music_list.length > 0) {
        return music_list;
    }
    return [];
}